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.
C#
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 MohantyRank #112 · 20K reputationPosted Nov 7, 2023, 8:29 AM EST
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.cmbRoom.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 nameUse parameterized query to prevent SQL injection:
private void cmbRoom_SelectedIndexChanged(object sender, EventArgs e) { if (cmbRoom.SelectedIndex != -1) { string qry = "SELECT rRate FROM tblRoom WHERE roomID = @selectedValue"; using (SqlCommand cmd = new SqlCommand(qry, MainClass.con)) { cmd.Parameters.AddWithValue("@selectedValue", cmbRoom.SelectedValue); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); da.Fill(dt); if (dt.Rows.Count > 0) { txtRate.Text = dt.Rows[0]["rRate"].ToString(); } } } } }Please post your question in the appropriate category, and place your code in the designated 'code' section rather than embedding it as text.
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 7, 2023, 10:59 AM EST
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
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 7, 2023, 11:23 AM EST
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.
Amit MohantyRank #112 · 20K reputationPosted Nov 8, 2023, 12:48 AM EST
Hey, some where in your form you have bind data to combobox(cmbRoom) right?. There you have to bind combobox like this:
cmbRoom.DataSource = yourDataSource; // Set your data source here cmbRoom.DisplayMember = "RoomName"; // Replace with the actual display column name cmbRoom.ValueMember = "RoomId"; // Replace with the actual value column nameEmmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 8, 2023, 4:07 AM EST
Hello Amit,
Thank you so much for your help,
Please I have enconter this error as I follow your formate,
Amit MohantyRank #112 · 20K reputationPosted Nov 8, 2023, 4:34 AM EST
public void BindComboBox(ComboBox comboBox) { string sqlQuery = "SELECT [roomId], [rName] FROM tblRoom"; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlQuery, MainClass.con); DataTable dataTable = new DataTable(); sqlDataAdapter.Fill(dataTable); comboBox.DataSource = dataTable; comboBox.DisplayMember = "rName"; comboBox.ValueMember = "roomId"; }Call the above method in your page load event to bind the combobox.
private void Form1_Load(object sender, EventArgs e) { BindComboBox(cmbRoom); // your existing code }private void cmbRoom_SelectedIndexChanged(object sender, EventArgs e) { if (cmbRoom.SelectedIndex != -1) { string qry = "SELECT rRate FROM tblRoom WHERE roomID = @selectedValue"; using (SqlCommand cmd = new SqlCommand(qry, MainClass.con)) { cmd.Parameters.AddWithValue("@selectedValue", cmbRoom.SelectedValue); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); da.Fill(dt); if (dt.Rows.Count > 0) { txtRate.Text = dt.Rows[0]["rRate"].ToString(); } } } } }Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 8, 2023, 6:02 AM EST
Hello Amit,
Please I enconter another error as shown in the picture.
Amit MohantyRank #112 · 20K reputationPosted Nov 8, 2023, 6:07 AM EST
On which line you got the error?
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 8, 2023, 6:18 AM EST
da.Fill(dt);
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 8, 2023, 6:40 AM EST
Hello Amit,
Please I enconter another error as shown in the picture.
Amit MohantyRank #112 · 20K reputationPosted Nov 8, 2023, 7:04 AM EST
Try this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.SelectedIndex != -1) { ComboBox comboBox = (ComboBox)sender; DataRowView selectedRow = (DataRowView)comboBox.SelectedItem; if (selectedRow != null) { string selectedValue = selectedRow["roomID"].ToString(); string sqlQuery = "SELECT rRate FROM tblRoom WHERE roomID = @selectedValue"; using (SqlConnection sqlConnection = new SqlConnection(connectionString)) { sqlConnection.Open(); using (SqlCommand cmd = new SqlCommand(sqlQuery, sqlConnection)) { cmd.Parameters.AddWithValue("@selectedValue", selectedValue); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); da.Fill(dt); if (dt.Rows.Count > 0) { txtRate.Text = dt.Rows[0]["rRate"].ToString(); } } } } } } }Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 8, 2023, 7:38 AM EST
Additional information: roomID is neither a DataColumn nor a DataRelation for table .
at this line string selectedValue = selectedRow["roomID"].ToString();