Click here to Skip to main content
14,826,929 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
     {
        DataSet1TableAdapters.TextBoxTableTableAdapter tx;
        tx = new DataSet1TableAdapters.TextBoxTableTableAdapter();
        DataTable dt = new DataTable();
        dt = tx.GetstudData(int.Parse(DropDownList1.SelectedValue));
        foreach (DataRow row in dt.Rows)
           {
           TextBox1.Text = (row["FirstName"]); // error shown here
           TextBox2.Text = (row["SecondName"]); // error shown here
           }
     }
Posted
Comments
[no name] 16-Oct-14 6:42am
   
Did you try row["FirstName"].ToString()); ?

[Updated]
The reason for the error is that a cell in a DataRow is of type object and you treat it as it would be a string.
Hence, you need to convert the object to a string.
[]

The simple solution is
C#
TextBox1.Text = row["FirstName"].ToString(); 


However, if the value in the DataRow cell is null, you will get a null pointer error.
To avoid that you can do like this:
C#
if (row["FirstName"] != null)
    TextBox1.Text = row["FirstName"].ToString();
else
    TextBox1.Text = "";  // Or what is suitable

or
C#
TextBox1.Text = (row["FirstName"] != null) ? row["FirstName"].ToString() : ""; 


If you are sure your DataRow cell contains a string, you can do an explicit type case
C#
TextBox1.Text = (string)row["FirstName"]; 

but this will also cause problems if the value of the cell is null and throw a type cast exception if the data type of the cell is not a string.
   
v4
Comments
[no name] 16-Oct-14 6:55am
   
I agree with George. Also,you can use
TextBox1.Text = Convert.ToString(row["FirstName"]);
This will handle the null too, unlike ToString() method.

Conversin is because a DataTable is of object type. You need to convert it before assigning it to some control. Else it will throw error
George Jonsson 16-Oct-14 7:09am
   
Good point. Forgot to explain the reason for the error.
you can try like this

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
     {
        DataSet1TableAdapters.TextBoxTableTableAdapter tx;
        tx = new DataSet1TableAdapters.TextBoxTableTableAdapter();
        DataTable dt = new DataTable();
        dt = tx.GetstudData(int.Parse(DropDownList1.SelectedValue));
        foreach (DataRow row in dt.Rows)
           {
           TextBox1.Text = (row["FirstName"]) as string;
           TextBox2.Text = (row["SecondName"]) as string; 
           }
     }
   
Comments
George Jonsson 16-Oct-14 6:51am
   
This will set the contents of the TextBox to null, if the data type of the cell is not a string.
This may, or may not be acceptable.
Mohamed Rafiq K 16-Oct-14 6:57am
   
yes of course he need to validate that the type of cell contains string

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900