Hello Team, Please I have this error in my C# window form application which says the multi-part identifier "System.Data.DataRowView could not be bound, Kindly help.
private void cmbRoom_SelectedIndexChanged(object sender, EventArgs e)
{
if(cmbRoom.SelectedIndex !=-1)
{
string qry = @"Select rRate from tblRoom where roomID ="+cmbRoom.SelectedValue+"";
SqlCommand cmd = new SqlCommand(qry,MainClass.con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
txtRate.Text = dt.Rows[0]["rRate"].ToString();
}
}
???????}
Amit MohantyPosted Nov 7, 2023, 1:29 PM
Note: Your code is vulnerable to SQL injection. You should use parameterized queries instead of directly embedding values into your SQL statements.
You should ensure that the ComboBox is properly bound to a data source, and the
ValueMemberproperty is set to the column that you want to use as the selected value.Use parameterized query to prevent SQL injection:
Please post your question in the appropriate category, and place your code in the designated 'code' section rather than embedding it as text.
Emmmanuel FIADUFEPosted Nov 8, 2023, 12:38 PM
Additional information: roomID is neither a DataColumn nor a DataRelation for table .
at this line string selectedValue = selectedRow["roomID"].ToString();
Amit MohantyPosted Nov 8, 2023, 12:04 PM
Try this:
Emmmanuel FIADUFEPosted Nov 8, 2023, 11:40 AM
Hello Amit,
Please I enconter another error as shown in the picture.
Emmmanuel FIADUFEPosted Nov 8, 2023, 11:18 AM
da.Fill(dt);
Amit MohantyPosted Nov 8, 2023, 11:07 AM
On which line you got the error?
Emmmanuel FIADUFEPosted Nov 8, 2023, 11:02 AM
Hello Amit,
Please I enconter another error as shown in the picture.
Amit MohantyPosted Nov 8, 2023, 9:34 AM
Call the above method in your page load event to bind the combobox.
Emmmanuel FIADUFEPosted Nov 8, 2023, 9:07 AM
Hello Amit,
Thank you so much for your help,
Please I have enconter this error as I follow your formate,
Amit MohantyPosted Nov 8, 2023, 5:48 AM
Hey, some where in your form you have bind data to combobox(cmbRoom) right?. There you have to bind combobox like this:
Emmmanuel FIADUFEPosted Nov 7, 2023, 4:23 PM
As you can see in the picture when I select room number then it would automatically select the rate from the Room dataGridview which is on a diferent page as you can see below, though everything seems to be working and that is the only problem am having now, so when I select different room number I want the rate change as well.
Emmmanuel FIADUFEPosted Nov 7, 2023, 3:59 PM
Thank you so much Amit, I have applied your format and am a bit confuse about what you mean by you should ensure that the ComboBox is properly bound to a data source, and the
ValueMemberproperty is set to the column that you want to use as the selected value and how I should apply this, kindly provide further clarification on how to go about it. thank you in advancecmbRoom.DataSource = yourDataSource; // Set your data source here
cmbRoom.DisplayMember = "displayColumnName"; // Replace with the actual display column name
cmbRoom.ValueMember = "valueColumnName"; // Replace with the actual value column name