Reputation: 47
I am a beginner to this so I am trying an example to understand so I really appreciate your help.
I have the following web application structure:
I have the following code for clicking the button "save" which saves the text written in the 3 text boxes into the 3 fields of the table Trial
in database:
protected void AddProgramButton_Click(object sender, System.EventArgs e)
{
DataBinder ds = new DataBinder();
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand("INSERT INTO Trial VALUES (@FirstColumn, @SecondColumn , @ThirdColumn)", sqlcon);
sqlcmd.Parameters.AddWithValue("@FirstColumn", RadTextBox1.Text);
sqlcmd.Parameters.AddWithValue("@SecondColumn", RadTextBox2.Text);
sqlcmd.Parameters.AddWithValue("@ThirdColumn", RadTextBox3.Text);
sqlcmd.ExecuteNonQuery();
// lbl.Visible = true;
sqlcon.Close();
}
My question is ::
The column FirstColumn
in the table Trial
is primary key field. So, I don't want to save the data into the database directly , BUT I want it to first check the value I entered in RadTextBox1
and see if it exists in the database or not ::
If it exists, then it will display the corresponding values in RadTextBox2
and RadTextBox3
If it doesn't exist , then RadTextBox2
and RadTextBox3
will remain blank and allow user to enter new data.
How can I do that ? I have been searching a lot but it is so confusing as I just started learning this. I reallyy appreciate your help.
Upvotes: 0
Views: 69
Reputation: 756
You can use datareader for this,
if(RadTextBox2.Text!=""||RadTextBox3.Text!="")
{
RadTextBox2.Text="";
RadTextBox3.Text="";
}
SqlConnection con = new SqlConnection("Your connection");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Trial where FirstColumn = '"+RadTextBox1.Text+"'",con);
SQlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
if(dr.HasRows())
{
RadTextBox2.Text = dr.GetString(1);
RadTextBox3.Text = dr.GetString(2);
}
else
{
}
}
con.Close();
It will come on your RadTextBox1 change event.Make sure your RadTextBox1 autopostback property set to true.
Upvotes: 1