Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

private void btnSave_Click(object sender, EventArgs e)
{
string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
SqlDataReader reader = null;
SqlConnection conn = null;

try
{
string query = "insert into CustomerTable values('" + txtCustomerName.Text + "','" + txtAddress.Text + "','"
+ txtMobileNo.Text + "','" + lblGoldBalance.Text + "','" + lblSilverBalance.Text + "','" + lblCashBalance.Text + "')";
conn = new SqlConnection(connstr);

if (txtCustomerName.Text != "" & txtAddress.Text != "")
{
conn.Open();
//Checking User Name Exists in CustomerTable

string query1 = "select txtCustomerName from CustomerTable where txtCustomerName='" + txtCustomerName.Text + "'";
SqlCommand cmd = new SqlCommand(query1,conn);
reader = cmd.ExecuteReader();

if (reader != null && reader.HasRows)
{
//User exists in db do something
MessageBox.Show("User Already Exists!!");
}

else
{
if (reader != null)
{
reader.Close(); // close the reader before making a new connection
}

SqlCommand cmd1 = new SqlCommand(query, conn);
cmd1.ExecuteNonQuery();
txtCustomerName.Clear();
txtAddress.Clear();
txtMobileNo.Clear();
MessageBox.Show("Values Save in DataBase");
}
reader.Close();
conn.Close();
}
else
{
MessageBox.Show("Only Mobile Field Can be Empty");
}
}
catch (Exception ex)
{
MessageBox.Show("At Least 0 value can be save in the Gold Silver Cash Area", ex.Message);
}
finally
{
if (reader != null)
{
reader.Close();
}

if (conn != null)
{
conn.Close();
}
}



it show an error when i m not write any value in MobileNo Field.
if put the value in all the textboxes then save the value perfectly. i want it show the "Only Mobile Field Can be Empty" message when it blank.so tell me what's the wrong in my code.

"Error converting data type varchar to numeric".
Posted

1 solution

You must check mobile number value before inserting into database(because it's numeric datatype in your db).
Check whether its null or not, it is then handle situation by writing different queries.
 
Share this answer
 
Comments
Manish Arya 3-Sep-13 4:55am    
yes its numeric and not null
Manish Arya 3-Sep-13 8:11am    
i m changing the datatype numeric to varchar.it's working.
but i want to write in numeric datatype and what is the different query.
discuss.saurabh 5-Sep-13 13:02pm    
instead of using txtMobileNo.Text, use Convert.ToInt64(txtMobileNo.Text)

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



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